package com.molyfun.parents.zhibo.im.event;

import android.text.TextUtils;

import com.molyfun.parents.im.model.BaseMessageEntity;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.zhibo.im.ImModel;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by william(liuchao.hit@gmail.com).
 * room start --> live start
 * room stop  --> live stop
 * room errorstop - > live errorstop
 * meeting invite --> live meetinginvite
 * meeting invitecancel -- > live meetinginvitecancel
 * meeting start  --> live meetingstart
 * meeting stop   --> live meetingstop
 * meeting apply  -->live meetingapply
 * meeting cancel	-->live meetingapplycancel
 * meeting confirm -->live meetingconfim
 * msg send       --> live groupmsg
 * gallery open    -->live  galleryopen
 * gallery close    -->live galleryclose
 * gallery seek     -->live galleryseek
 * gallery pause     -->live gallerypause
 * <p>
 * <p>
 * room enter --> live enter
 * room leave --> live leave
 * <p>
 * 新增
 * 小组群聊
 * group groupmsg
 * group singlemsg
 * group livestart
 * group livestop
 * <p>
 * 咨询
 * customplan groupmsg
 * customplan refreshcount
 * <p>
 * 登陆
 * login  imloginout
 * <p>
 * <p>
 * 接口调整
 * <p>
 * 7.直播事件
 * <p>
 * http://127.0.0.1/hello/api/v1/live.do?event
 * <p>
 * <p>
 * 现有的事件:
 * eventtype:live    opertype:galleryopen       //素材打开
 * opertype:galleryclose      //素材关闭
 * opertype:gallerypause      //素材暂停
 * opertype:galleryseek       //素材快进
 */

public class EventHandler {

    public static void handlerEvent(String msg, EventExecuter eventExecuter) {

        System.out.println("msg--->" + msg);

        if (!TextUtils.isEmpty(msg) && eventExecuter != null) {
            try {
                JSONObject jsonObject = new JSONObject(msg);

                String eventType = jsonObject.getString("eventtype");
                String operType = jsonObject.getString("opertype");

                JSONObject dataObject = null;
                if (jsonObject.has("data")) {
                    dataObject = jsonObject.getJSONObject("data");
                }
                if (!TextUtils.isEmpty(eventType)) {
                    System.out.println("eventType--->" + eventType);
                    System.out.println("operType--->" + operType);
                    switch (eventType) {
                        case "live":

                            if (!TextUtils.isEmpty(operType)) {
//                                ImModel.GalleryBody galleryBody = GsonUtil.fromJson(StringUtils.decode(jsonObject.getString("data")), ImModel.GalleryBody.class);
                                ImModel.GalleryBody galleryBody = GsonUtil.fromJson(dataObject.toString(), ImModel.GalleryBody.class);
                                switch (operType) {
                                    case "galleryopen":
                                        eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeOpen);
                                        break;
                                    case "galleryclose":
                                        eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeClose);
                                        break;
                                    case "gallerypause":
                                        eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypePause);
                                        break;
                                    case "galleryseek":
                                        eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeSeek);
                                        break;
                                    case "enter":
                                        eventExecuter.onMessageNewUser(GsonUtil.fromJson(jsonObject.toString(), ImModel.UserCountModel.class));
                                        break;
                                    case "leave":
                                        eventExecuter.onMessageUserOut(GsonUtil.fromJson(jsonObject.toString(), ImModel.UserCountModel.class));
                                        break;
                                    case "stop":
                                        eventExecuter.onTeacherLeave();
                                        break;
                                    case "errorstop":
                                        eventExecuter.onTeacherLeave();
                                        break;
                                    case "start":
                                        eventExecuter.onTeacherEnter();
                                        break;
                                    case "meetinginvite":
                                        eventExecuter.onMessageRtcInvite(GsonUtil.fromJson(jsonObject.toString(), ImModel.RtcInviteModel.class));
                                        break;
                                    case "meetingconfim":
                                        eventExecuter.onMessageRtcConfim(GsonUtil.fromJson(jsonObject.toString(), ImModel.RtcInviteModel.class));
                                        break;
                                    case "meetingstart":
                                        eventExecuter.onMessageRtcStart(GsonUtil.fromJson(jsonObject.toString(), ImModel.RtcStartModel.class));
                                        break;
                                    case "meetinginvitecancel":
                                        eventExecuter.onMessageRtcCancel(GsonUtil.fromJson(jsonObject.toString(), ImModel.RtcInviteModel.class));
                                        break;
                                    case "meetingstop":
                                        eventExecuter.onMessageRtcStop();
                                        break;
                                    case "groupmsg":
                                        eventExecuter.onMessageNewStr(GsonUtil.fromJson(jsonObject.toString(), ImModel.ImStringModel.class));
                                        break;
                                }
                            }
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    public static void handlerEvent(BaseMessageEntity baseMessageEntity, EventExecuter eventExecuter) {

        if (baseMessageEntity != null && eventExecuter != null) {

            String eventType = baseMessageEntity.getEventtype();
            String operType = baseMessageEntity.getOpertype();

            if (!TextUtils.isEmpty(eventType)) {
                System.out.println("eventType--->" + eventType);
                System.out.println("operType--->" + operType);
                switch (eventType) {
                    case "live":

                        if (!TextUtils.isEmpty(operType)) {
//                                ImModel.GalleryBody galleryBody = GsonUtil.fromJson(StringUtils.decode(jsonObject.getString("data")), ImModel.GalleryBody.class);
                            ImModel.GalleryBody galleryBody = GsonUtil.fromJson(baseMessageEntity.getData(), ImModel.GalleryBody.class);
                            switch (operType) {
                                case "galleryopen":
                                    eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeOpen);
                                    break;
                                case "galleryclose":
                                    eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeClose);
                                    break;
                                case "gallerypause":
                                    eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypePause);
                                    break;
                                case "galleryseek":
                                    eventExecuter.onMessageGalleryOperate(galleryBody, ImModel.GalleryOperateType.OperateTypeSeek);
                                    break;
                                case "enter":
                                    eventExecuter.onMessageNewUser(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.UserCountModel.class));
                                    break;
                                case "leave":
                                    eventExecuter.onMessageUserOut(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.UserCountModel.class));
                                    break;
                                case "stop":
                                    eventExecuter.onTeacherLeave();
                                    break;
                                case "errorstop":
                                    eventExecuter.onTeacherLeave();
                                    break;
                                case "start":
                                    eventExecuter.onTeacherEnter();
                                    break;
                                case "meetinginvite":
                                    eventExecuter.onMessageRtcInvite(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.RtcInviteModel.class));
                                    break;
                                case "meetingconfim":
                                    eventExecuter.onMessageRtcConfim(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.RtcInviteModel.class));
                                    break;
                                case "meetingstart":
                                    eventExecuter.onMessageRtcStart(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.RtcStartModel.class));
                                    break;
                                case "meetinginvitecancel":
                                    eventExecuter.onMessageRtcCancel(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.RtcInviteModel.class));
                                    break;
                                case "meetingstop":
                                    eventExecuter.onMessageRtcStop();
                                    break;
                                case "groupmsg":
                                    eventExecuter.onMessageNewStr(GsonUtil.fromJson(GsonUtil.toJson(baseMessageEntity), ImModel.ImStringModel.class));
                                    break;
                            }
                        }
                }
            }


        }
    }

}
